package com.example.hp.yuejian5.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hp.yuejian5.MainActivity;
import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.RetrofitListener.RetrofitListener;
import com.example.hp.yuejian5.bean.LoginBean;
import com.example.hp.yuejian5.model.UserModel;


public class LoginActivity extends AppCompatActivity {

    private Button login_button;
    private EditText username, password;
    private TextView forget_password, register;
    private RetrofitListener<LoginBean> listener;
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        viewListener();
        listener=new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean, int flag) {
               if(loginBean.getUser_id().equals("0"))
               {
                   Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
               }
               else
               {
                   saveUid(loginBean);
                   Intent intent=new Intent(LoginActivity.this,FragmentActivity.class);

                   startActivity(intent);
                   finish();
               }
            }

            @Override
            public void onFail() {
                Toast.makeText(LoginActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();
            }
        };

    }
    public void saveUid(LoginBean list){
        String u_id=list.getUser_id();
        sp = getSharedPreferences("user_id", MODE_PRIVATE);//默认操作模式
        SharedPreferences.Editor editor = sp.edit();//通过edit()获取Editor对象

        editor.putString("userid",u_id);//通过editor对象存储键值对的操作
        editor.commit();//通过commit()提交数据
    }
    private void initView() {
        login_button = findViewById(R.id.login_button);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        forget_password = findViewById(R.id.forget_password);
        register = findViewById(R.id.register_login);
    }

    private void viewListener() {
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent (LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
    void login()
    {
        UserModel userModel=new UserModel();
        userModel.userLogin(username.getText().toString().trim(),password.getText().toString().trim(),listener);
    }
}
